﻿using System;
using System.Collections.Generic;
using System.IO;
using log4net;
using log4net.Config;

namespace MudEngine
{
    public class Log
    {
        /// <summary>
        /// 
        /// </summary>
        private static Dictionary<Type, ILog> _loggers = new Dictionary<Type, ILog>();
		private static bool _logConsole;
		private static bool _configured = false;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configFile"></param>
        public static void Init(string configFile, bool console)
        {
            FileInfo fileInfo = new FileInfo(configFile);
            GlobalContext.Properties["LogName"] = AppDomain.CurrentDomain.FriendlyName;

            XmlConfigurator.Configure(fileInfo);
			_logConsole = console;
			_configured = true;
            GetLogger(typeof(Log)).Info(string.Format("Log class loaded in domain {0}.", AppDomain.CurrentDomain.FriendlyName));
        }
		/// <summary>
		/// 
		/// </summary>
		public static void Init()
		{
			Init("logConfig.xml", true);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="dType"></param>
		/// <param name="type"></param>
		/// <param name="message"></param>
		private static void Console(string dType, Type type, string message)
		{
			if (_logConsole)
				//System.Console.WriteLine("[{0}|{1}] {2}", dType, type.Name, message);
                System.Console.WriteLine("[{0}] {1}", dType, message);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="message"></param>
        public static void Debug(Type type, string message)
        {
            ILog logger = GetLogger(type);
            if (logger.IsDebugEnabled)
            {
                logger.Debug(message);
				Console("D", type, message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Debug(Type type, string message, Exception exception)
        {
            ILog logger = GetLogger(type);
            if (logger.IsDebugEnabled)
            {
                logger.Debug(message, exception);
				Console("D", type, message);
			}
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="message"></param>
        public static void Info(Type type, string message)
        {
            ILog logger = GetLogger(type);
            if (logger.IsInfoEnabled)
            {
                logger.Info(message);
				Console("I", type, message);
			}
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Info(Type type, string message, Exception exception)
        {
            ILog logger = GetLogger(type);
            if (logger.IsInfoEnabled)
            {
                logger.Info(message, exception);
				Console("I", type, message);
			}
        }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="message"></param>
		public static void Fatal(Type type, string message)
		{
			ILog logger = GetLogger(type);

			if (logger.IsFatalEnabled)
			{
				logger.Fatal(message);
				Console("E", type, message);
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="message"></param>
		/// <param name="exception"></param>
		public static void Fatal(Type type, string message, Exception exception)
		{
			ILog logger = GetLogger( type );
			
			if (logger.IsFatalEnabled)
			{
				logger.Fatal(message, exception);
				Console("E", type, message);
			}
		}
		/// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ILog GetLogger(Type type)
        {
			if (!_configured)
				Init();

            ILog logger = null;
            if ( _loggers.TryGetValue( type, out logger ) )
            {
                return logger;
            }

            logger = LogManager.GetLogger(type);
            _loggers.Add(type, logger);

            return logger;
        }
    }
}
